/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const validMap = ['admin', 'editor']
  return validMap.indexOf(str.trim()) >= 0
}

/**
 * @param {string} url
 * @returns {Boolean}
 */
export function validURL(url) {
  const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return reg.test(url)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
  // eslint-disable-next-line
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function isString(str) {
  if (typeof str === 'string' || str instanceof String) {
    return true
  }
  return false
}

/**
 * @param {Array} arg
 * @returns {Boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}
export function checkPhone(val) {
  if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(val))) {
    return false
  } else {
    return true
  }
}

export function getCarIdHidden(val) {
  let carId = ''
  carId = val.replace(/^(.{4})(?:\d+)(.{4})$/, '$1******$2')
  return carId
}
export function processName(str) {
  if (str !== null && str !== undefined) {
    let star = '' // 存放名字中间的*
    // 名字是两位的就取姓名首位+*
    if (str.length <= 2) {
      return str.substring(0, 1) + '*'
    } else {
      for (var i = 0; i < str.length - 2; i++) {
        star = star + '*'
      }
      return str.substring(0, 1) + star + str.substring(str.length - 1, str.length)
    }
  }
}
// 香港身份证号码校验
// 开头一位或两位大写字母，然后接上6-10位数字，最后一位数字或字母校验
export function isMatchHongKong(identityCard) {
  var str = /^[A-Z]{1,2}[0-9]{6,10}[0-9A-Z]$/
  return str.test(identityCard)
}

// 澳门身份证号码校验
// 开头数字1或者5或者7，然后接上6位数字，再接上一位数字或者大写字母校验
export function isMatchAoMen(identityCard) {
  var str = /^[1|5|7][0-9]{6}[0-9A-Z]$/
  return str.test(identityCard)
}
// 台湾身份证号码校验
// 开头一位小写或者大写字母，接上9位数字
export function isMatchTaiWan(identityCard) {
  var str = /^[a-zA-Z][0-9]{9}$/
  return str.test(identityCard)
}
export const getMessageByMsg = (data, config) => {
  const id = config.id || 'id'
  const pid = config.pid || 'parentId'
  const children = config.children || 'children'
  const idMap = {}
  const jsonTree = []
  data.forEach(function(v) {
    idMap[v[id]] = v
  })
  data.forEach(function(v) {
    const parent = idMap[v[pid]]
    if (parent) {
      !parent[children] && (parent[children] = [])
      v.flag = 0
      parent[children].push(v)
    } else {
      v.flag = 1
      jsonTree.push(v)
    }
  })
  return jsonTree
}
